<?php
class ModelRestaurantRestaurant extends Model {
	public function addRestaurant($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant SET `language` = '" . $this->db->escape($data['language']) . "', `currency` = '" . $this->db->escape($data['currency']) . "', `url` = '" . $this->db->escape($data['config_url']) . "', `ssl` = '" . $this->db->escape($data['config_ssl']) . "', `status` = '" . (int)$data['status'] . "', date_added = NOW()");

		$restaurant_id = $this->db->getLastId();

		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "restaurant SET image = '" . $this->db->escape($data['image']) . "' WHERE restaurant_id = '" . (int)$restaurant_id . "'");
		}

		foreach ($data['description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_description SET restaurant_id = '" . (int)$restaurant_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}
	
		foreach ($data['addresses'] as $address) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_address SET restaurant_id = '" . (int)$restaurant_id . "', country_id = '" . (int)$address['country_id'] . "', city_id = '" . (int)$address['city_id'] . "', district_id = '" . (int)$address['district_id'] . "', address = '" . $this->db->escape($address['address']) . "'");
		}
		
		if (isset($data['restaurant_option'])) {
			foreach ($data['restaurant_option'] as $restaurant_option) {
				if ($restaurant_option['type'] == 'select' || $restaurant_option['type'] == 'radio' || $restaurant_option['type'] == 'checkbox' || $restaurant_option['type'] == 'image') {
					if (isset($restaurant_option['restaurant_option_value'])) {
						foreach ($restaurant_option['restaurant_option_value'] as $restaurant_option_value) {
							$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_option SET restaurant_id = '" . (int)$restaurant_id . "', option_id = '" . (int)$restaurant_option['option_id'] . "', option_value_id = '" . $this->db->escape($restaurant_option_value) . "'");
						}
					}
				} else {
					$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_option SET restaurant_id = '" . (int)$restaurant_id . "', option_id = '" . (int)$restaurant_option['option_id'] . "', option_value = '" . $this->db->escape($restaurant_option['option_value']) . "'");
				}
			}
		}

		if (isset($data['more_image'])) {
			foreach ($data['more_image'] as $more_image) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_image SET restaurant_id = '" . (int)$restaurant_id . "', image = '" . $this->db->escape($more_image['image']) . "', sort_order = '" . (int)$more_image['sort_order'] . "'");
			}
		}

		foreach ($data['restaurant_tag'] as $language_id => $value) {
			if ($value) {
				$tags = explode(',', $value);

				foreach ($tags as $tag) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_tag SET restaurant_id = '" . (int)$restaurant_id . "', language_id = '" . (int)$language_id . "', tag = '" . $this->db->escape(trim($tag)) . "'");
				}
			}
		}

		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'restaurant_id=" . (int)$restaurant_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}

		$this->cache->delete('restaurant');
		$this->cache->delete('option');

		return $restaurant_id;
	}

	public function editRestaurant($restaurant_id, $data) {
		$sql  = "UPDATE " . DB_PREFIX . "restaurant SET `language` = '" . $this->db->escape($data['language']) . "', `currency` = '" . $this->db->escape($data['currency']) . "', `url` = '" . $this->db->escape($data['config_url']) . "', `ssl` = '" . $this->db->escape($data['config_ssl']) . "', date_modified = NOW()";

		if(isset($data['status'])) {
			$sql .= " , `status` = '" . (int)$data['status'] . "'";
		}

		$sql .= " WHERE restaurant_id = '" . (int)$restaurant_id . "'";

		$this->db->query($sql);

		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "restaurant SET image = '" . $this->db->escape($data['image']) . "' WHERE restaurant_id = '" . (int)$restaurant_id . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "restaurant_description WHERE restaurant_id = '" . (int)$restaurant_id . "'");

		foreach ($data['description'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_description SET restaurant_id = '" . (int)$restaurant_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "'");
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "restaurant_address WHERE restaurant_id = '" . (int)$restaurant_id . "'");
		
		foreach ($data['addresses'] as $address) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_address SET restaurant_id = '" . (int)$restaurant_id . "', country_id = '" . (int)$address['country_id'] . "', city_id = '" . (int)$address['city_id'] . "', district_id = '" . (int)$address['district_id'] . "', address = '" . $this->db->escape($address['address']) . "'");
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "restaurant_option WHERE restaurant_id = '" . (int)$restaurant_id . "'");

		if (isset($data['restaurant_option'])) {
			foreach ($data['restaurant_option'] as $restaurant_option) {
				if ($restaurant_option['type'] == 'select' || $restaurant_option['type'] == 'radio' || $restaurant_option['type'] == 'checkbox' || $restaurant_option['type'] == 'image') {
					if (isset($restaurant_option['restaurant_option_value'])) {
						foreach ($restaurant_option['restaurant_option_value'] as $restaurant_option_value) {
							$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_option SET restaurant_id = '" . (int)$restaurant_id . "', option_id = '" . (int)$restaurant_option['option_id'] . "', option_value_id = '" . $this->db->escape($restaurant_option_value) . "'");
						}
					}
				} else {
					$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_option SET restaurant_id = '" . (int)$restaurant_id . "', option_id = '" . (int)$restaurant_option['option_id'] . "', option_value = '" . $this->db->escape($restaurant_option['option_value']) . "'");
				}
			}
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "restaurant_image WHERE restaurant_id = '" . (int)$restaurant_id . "'");

		if (isset($data['more_image'])) {
			foreach ($data['more_image'] as $more_image) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_image SET restaurant_id = '" . (int)$restaurant_id . "', image = '" . $this->db->escape($more_image['image']) . "', sort_order = '" . (int)$more_image['sort_order'] . "'");
			}
		}

		$this->db->query("DELETE FROM " . DB_PREFIX . "restaurant_tag WHERE restaurant_id = '" . (int)$restaurant_id . "'");

		foreach ($data['restaurant_tag'] as $language_id => $value) {
			if ($value) {
				$tags = explode(',', $value);

				foreach ($tags as $tag) {
					$this->db->query("INSERT INTO " . DB_PREFIX . "restaurant_tag SET restaurant_id = '" . (int)$restaurant_id . "', language_id = '" . (int)$language_id . "', tag = '" . $this->db->escape(trim($tag)) . "'");
				}
			}
		}
			
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'restaurant_id=" . (int)$restaurant_id. "'");

		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'restaurant_id=" . (int)$restaurant_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}

		$this->cache->delete('restaurant');
		$this->cache->delete('option');
	}

	public function getRestaurant($restaurant_id) {
		$sql  = "SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'restaurant_id=" . (int)$restaurant_id . "') AS keyword FROM " . DB_PREFIX . "restaurant r LEFT JOIN " . DB_PREFIX . "restaurant_description rd ON r.restaurant_id = rd.restaurant_id";
		$sql .= " WHERE r.restaurant_id = '" . (int)$restaurant_id . "'";
		$sql .= " AND rd.language_id = '" . (int)$this->config->get('config_language_id') . "'";

		$query = $this->db->query($sql);

		return $query->row;
	}

	public function getRestaurantDescriptions($restaurant_id){
		$description_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "restaurant_description WHERE restaurant_id = '" . (int)$restaurant_id . "'");

		foreach ($query->rows as $result) {
			$description_data[$result['language_id']] = array(
				'name'             => $result['name'],
				'description'      => $result['description'],
				'meta_keyword'     => $result['meta_keyword'],
				'meta_description' => $result['meta_description']
			);
		}

		return $description_data;
	}

	public function getRestaurantAddresses($restaurant_id){
		$address_data = array();
		
		$sql  = "SELECT ra.*, c.name AS country, ct.name AS city, d.name AS district FROM " . DB_PREFIX . "restaurant_address ra";
		$sql .= " LEFT JOIN " . DB_PREFIX . "country c ON (ra.country_id = c.country_id)";
		$sql .= " LEFT JOIN " . DB_PREFIX . "city ct ON (ra.city_id = ct.city_id)";
		$sql .= " LEFT JOIN " . DB_PREFIX . "district d ON (ra.district_id = d.district_id)";
		$sql .= " WHERE restaurant_id = '" . (int)$restaurant_id . "'";
		
		$query = $this->db->query($sql);

		foreach ($query->rows as $result) {
			$address_data[] = array(
				'address_id'             => $result['address_id'],
				'country_id'      => $result['country_id'],
				'city_id'     => $result['city_id'],
				'district_id' => $result['district_id'],
				'address' => $result['address'],
				'country' => $result['country'],
				'city' => $result['city'],
				'district' => $result['district']
			);
		}
		
		return $address_data;
	}
	
	public function getRestaurantOptions($restaurant_id) {
		$restaurant_option_data = array();

		$restaurant_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "restaurant_option ro LEFT JOIN `" . DB_PREFIX . "option` o ON (ro.option_id = o.option_id) LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id) WHERE ro.restaurant_id = '" . (int)$restaurant_id . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");

		foreach ($restaurant_option_query->rows as $restaurant_option) {
			if ($restaurant_option['type'] == 'select' || $restaurant_option['type'] == 'radio' || $restaurant_option['type'] == 'checkbox' || $restaurant_option['type'] == 'image') {
				if(!isset($restaurant_option_data[$restaurant_option['option_id']])) {
					$restaurant_option_data[$restaurant_option['option_id']] = array(
						'option_id'            => $restaurant_option['option_id'],
						'name'                 => $restaurant_option['name'],
						'type'                 => $restaurant_option['type']
					);
						
					$restaurant_option_value_data = array();
						
					$restaurant_option_value_query = $this->db->query("SELECT option_value_id FROM " . DB_PREFIX . "restaurant_option WHERE option_id = '" . (int)$restaurant_option['option_id'] . "' AND restaurant_id = '$restaurant_id'");
						
					foreach ($restaurant_option_value_query->rows as $restaurant_option_value) {
						$restaurant_option_value_data[] = $restaurant_option_value['option_value_id'];
					}
						
					$restaurant_option_data[$restaurant_option['option_id']]['restaurant_option_value'] = $restaurant_option_value_data;
				}
			} else {
				$restaurant_option_data[] = array(
					'option_id'         => $restaurant_option['option_id'],
					'name'              => $restaurant_option['name'],
					'type'              => $restaurant_option['type'],
					'option_value'      => $restaurant_option['option_value']
				);
			}
		}

		return $restaurant_option_data;
	}

	public function getRestaurantImages($restaurant_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "restaurant_image WHERE restaurant_id = '" . (int)$restaurant_id . "'");

		return $query->rows;
	}

	public function getRestaurants($data = array()) {
		if($data) {
			$sql  = "SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'restaurant_id'=r.restaurant_id) AS keyword FROM " . DB_PREFIX . "restaurant r LEFT JOIN " . DB_PREFIX . "restaurant_description rd ON r.restaurant_id = rd.restaurant_id";
			$sql .= " WHERE r.restaurant_id != 0";
			$sql .= " AND rd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
				
			$query = $this->db->query($sql);

			return $query->rows;
		}else{
			$key = 'restaurant.' . (int)$this->config->get('config_language_id');
			$restaurant_data = $this->cache->get($key);
				
			if (!$restaurant_data) {
				$sql  = "SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'restaurant_id'=r.restaurant_id) AS keyword FROM " . DB_PREFIX . "restaurant r LEFT JOIN " . DB_PREFIX . "restaurant_description rd ON r.restaurant_id = rd.restaurant_id";
				$sql .= " WHERE r.restaurant_id != 0";
				$sql .= " AND rd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
					
				$query = $this->db->query($sql);
					
				$restaurant_data = $query->rows;
					
				$this->cache->set($key, $restaurant_data);
			}
				
			return $restaurant_data;
		}
	}

	public function getRestaurantsForAutocomplete($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "restaurant r LEFT JOIN " . DB_PREFIX . "restaurant_description rd ON r.restaurant_id = rd.restaurant_id WHERE r.restaurant_id != '0'";
		$sql .= " AND rd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		
		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND LCASE(rd.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
		}

		if(isset($data['ignore_restaurant_ids']) && strlen($data['ignore_restaurant_ids']) > 0){
			$sql .= " AND r.restaurant_id NOT IN ( " . $data['ignore_restaurant_ids'] . ")";
		}

		$sort_data = array(
			'name'
			);

			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];
			} else {
				$sql .= " ORDER BY rd.name";
			}

			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}

			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}

				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}

			$query = $this->db->query($sql);

			$restaurant_data = $query->rows;

			return $restaurant_data;
	}

	public  function getRestaurantsByUserId($user_id){
		$sql = "SELECT * FROM (" . DB_PREFIX . "restaurant r" .
		       " LEFT JOIN " . DB_PREFIX . "restaurant_description rd ON (r.restaurant_id = rd.restaurant_id))" .
			   " INNER JOIN " . DB_PREFIX . "user_to_restaurant us" .
			   " ON r.restaurant_id = us.restaurant_id" .
			   " WHERE r.restaurant_id != '0' AND user_id = '" . (int)$user_id . "'" . 
			   " AND rd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
		$sql .= " ORDER BY url";


		$query = $this->db->query($sql);

		$restaurant_data = $query->rows;

		return $restaurant_data;
	}

	public function getRestaurantsByTemplateId($template_id) {
		$template_restaurant_data = array();

		$sql  = "SELECT DISTINCT * FROM (" . DB_PREFIX . "restaurant r" .
				" LEFT JOIN " . DB_PREFIX . "restaurant_description rd ON (r.restaurant_id = rd.restaurant_id))" .
				" INNER JOIN " . DB_PREFIX . "template_to_restaurant tts ON r.restaurant_id = tts.restaurant_id" .
				" WHERE tts.template_id = '" . (int)$template_id . "'" .
				" AND rd.language_id = '" . (int)$this->config->get('config_language_id') . "'";

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalRestaurants() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "restaurant WHERE restaurant_id != '0'");

		return $query->row['total'];
	}

	public function getRestaurantTags($restaurant_id) {
		$restaurant_tag_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "restaurant_tag WHERE restaurant_id = '" . (int)$restaurant_id . "'");

		$tag_data = array();

		foreach ($query->rows as $result) {
			$tag_data[$result['language_id']][] = $result['tag'];
		}

		foreach ($tag_data as $language => $tags) {
			$restaurant_tag_data[$language] = implode(',', $tags);
		}

		return $restaurant_tag_data;
	}

	public function getTotalRestaurantsByLanguage($language) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "setting WHERE `key` = 'config_language' AND `value` = '" . $this->db->escape($language) . "' AND restaurant_id != '0'");

		return $query->row['total'];
	}

	public function getTotalRestaurantsByCurrency($currency) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "setting WHERE `key` = 'config_currency' AND `value` = '" . $this->db->escape($currency) . "' AND restaurant_id != '0'");

		return $query->row['total'];
	}

	public function getTotalRestaurantsByCountryId($country_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "setting WHERE `key` = 'config_country_id' AND `value` = '" . (int)$country_id . "' AND restaurant_id != '0'");

		return $query->row['total'];
	}

	public function getTotalRestaurantsByOptionId($option_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "restaurant_option WHERE option_id = '" . (int)$option_id . "' AND option_value_id > 0");

		return $query->row['total'];
	}

	public function getTotalRestaurantsByMenuAttributeId($attribute_id){
		$sql  = "SELECT COUNT(DISTINCT restaurant_id) AS total FROM " . DB_PREFIX . "restaurant_menu_attribute rma";
		$sql .= " INNER JOIN " . DB_PREFIX . "restaurant_menu rm ON (rma.menu_id = rm.menu_id)";
		$sql .= " WHERE attribute_id = '" . (int)$attribute_id . "'";
		$query = $this->db->query($sql);

		return $query->row['total'];
	}

	public function getTotalRestaurantsByCityId($city_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "setting WHERE `key` = 'config_city_id' AND `value` = '" . (int)$city_id . "' AND restaurant_id != '0'");

		return $query->row['total'];
	}
	
	public function getTotalRestaurantsByOrderStatusId($order_status_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "setting WHERE `key` = 'config_order_status_id' AND `value` = '" . (int)$order_status_id . "' AND restaurant_id != '0'");
		
		return $query->row['total'];		
	}	
	
	public function getTotalRestaurantsByBookingStatusId($booking_status_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "setting WHERE `key` = 'config_booking_status_id' AND `value` = '" . (int)$booking_status_id . "' AND restaurant_id != '0'");
		
		return $query->row['total'];		
	}	
}
?>